//
//  CSYEmoticonManager.swift
//  CSYEmoticonKeyboard__Test
//
//  Created by 姚彦兆 on 15/11/21.
//  Copyright © 2015年 姚彦兆. All rights reserved.
//

import UIKit

class CSYEmoticonManager: NSObject {

    static let manager : CSYEmoticonManager = CSYEmoticonManager()
    
    lazy var packageArray : [CSYEmoticonPackageModel] = [CSYEmoticonPackageModel]()
    
    func setTextToImageText ( targetText : String , font : UIFont ) -> NSAttributedString {
    
        let str : String = targetText
        
        //需要使用正则表达式来取到文本中的对应图片值
        //设置解决方案
        //表情字符串中的[]是正则表达式中的特殊字符,需要转译
        let pattern : String = "\\[.*?\\]"
        
        //使用解决方案实例化正则表达式
        let regularExpression : NSRegularExpression = try! NSRegularExpression(pattern: pattern, options: [])
        
        //使用正则表达式的条件遍历选择符合要求的字符串
        let result = regularExpression.matchesInString(str, options: [], range: NSRange(location: 0, length: str.characters.count))
        
        let attributeString : NSMutableAttributedString = NSMutableAttributedString(string: str)
        
        var count : NSInteger = result.count - 1
        
        while count >= 0 {
            
            let range = result[count].rangeAtIndex(0)
            
            let targetStr = (str as NSString).substringWithRange(range)

            //获取到了每一个表情的字符串,去对应的表情数组中获取该字符串对应的表情
            
            if let emoticon : CSYEmoticonModel = getEmoticonWithStr(targetStr){
                
                let imageText : NSAttributedString = CSYTextAttachment().getEmoticonAttachmentText(emoticon , font : font)
                
                attributeString.replaceCharactersInRange(range, withAttributedString: imageText)
                
            }
            
            count--
            
        }
        
        return attributeString
    
    }
    
    //根据表情字符串匹配表情对象
    func getEmoticonWithStr( emStr : String ) -> CSYEmoticonModel?{
        
        for package in CSYEmoticonManager.manager.packageArray{
            
            //设置数组过滤器,选择对应条件
            
            let model = package.emoticonModelArray.filter({ (allItem) -> Bool in
                
                return allItem.chs == emStr
                
            })
            
            if let item : CSYEmoticonModel = model.last{
                
                return item
                
            }
            
        }
        
        return nil
        
    }
    
    override init() {
        
        super.init()
        
        getEmoticonPackageMessage()
        
    }
    
    func getEmoticonPackageMessage(){
    
        let path : String = NSBundle.mainBundle().pathForResource("emoticons", ofType: "plist", inDirectory: "Emoticons.bundle")!
        
        let dict = NSDictionary(contentsOfFile: path)
        
        if let array : [[String : AnyObject]] = dict!["packages"] as? [[String : AnyObject]] {
        
            for dict in array{
            
                let id : String = dict["id"] as! String
            
                getemoticonPlist(id)
                
            }
        
        }
        
    }
    
    private func getemoticonPlist (id : String){
        
        let path : String = NSBundle.mainBundle().pathForResource("Info", ofType: "plist", inDirectory: "Emoticons.bundle/\(id)")!
        
        let dict = NSDictionary(contentsOfFile: path)

        let groupName : String = dict!["group_name_cn"] as! String
        
        let plistData : [[String : AnyObject]] = dict!["emoticons"] as! [[String : AnyObject]]
        
        let model : CSYEmoticonPackageModel = CSYEmoticonPackageModel(id: id, group_name_cn: groupName, plistData: plistData)
    
        packageArray.append(model)
    
    }
    
    
    
}
